﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SnakesAndLaddersMod
{
    public class MenuScreen : GameScreen
    {
        #region Fields

        List<MenuItem> menuItems = new List<MenuItem>();
        public int selectedEntry = 0;
        string menuTitle;

        protected IList<MenuItem> MenuItems
        {
            get { return menuItems; }
        }

        #endregion

        public MenuScreen(string title)
        {
            this.menuTitle = title;

            TransitionOnTime = TimeSpan.FromSeconds(0.5);
            TransitionOffTime = TimeSpan.FromSeconds(0.5);
        }

        public override void HandleInput(InputHandler input)
        {
            if (input.IsMenuUp())
            {
                selectedEntry--;

                if (selectedEntry < 0)
                {
                    selectedEntry = menuItems.Count - 1;
                }
            }

            if (input.IsMenuDown())
            {
                selectedEntry++;

                if (selectedEntry >= menuItems.Count)
                {
                    selectedEntry = 0;
                }
            }

            if (input.IsMenuSelect())
            {
                OnSelectEntry();
            }

            if (input.IsMenuBack())
            {
                OnCancel();
            }
        }

        protected virtual void OnSelectEntry()
        {
            menuItems[selectedEntry].OnSelectEntry();
        }

        protected virtual void OnCancel()
        {
            ExitScreen();
        }

        protected void OnCancel(object sender)
        {
            OnCancel();
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);

            for (int i = 0; i < menuItems.Count; i++)
            {
                bool isSelected = IsActive && (i == selectedEntry);

                menuItems[i].Update(this, isSelected, gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            Vector2 position = new Vector2(100, 150);

            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            if (ScreenState == ScreenState.TransitionOn)
                position.X -= transitionOffset * 256;
            else
                position.X += transitionOffset * 512;

            spriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < menuItems.Count; i++)
            {
                MenuItem menuEntry = menuItems[i];

                bool isSelected = IsActive && (i == selectedEntry);

                menuEntry.Draw(this, position, isSelected, gameTime);

                position.Y += menuEntry.GetHeight(this);
            }

            // Draw the menu title.
            Vector2 titlePosition = new Vector2(426, 80);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192, TransitionAlpha);
            float titleScale = 1.25f;

            titlePosition.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
                                   titleOrigin, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }
    }
}
